#include "dispenser.h"
#include <pthread.h>

void *set(void *args)
{
    int ret = -1;
    int i = 0;
    char msg[128] = "";
    struct dispenser_config *dispenser = (struct dispenser_config *)args;

    for (; i < 10000000; i++) {
        memset(msg, 0x00, 128);
        snprintf(msg, sizeof(msg), "no.%d data", i);
        ret = dispenser_set_data(dispenser, msg);  
        if (-1 == ret) {
            perror("dispenser_set_data");
        }
        usleep(30000);
    }
    return NULL;
}

void *get1(void *args)
{
    int ret = -1;
    struct dispenser_config *dispenser = (struct dispenser_config *)args;
    char rsp[128] = "";

    while (1) {
        ret = dispenser_get_data(dispenser, rsp);  
        if (-1 == ret) {
        continue;
        }
        printf("[1]the dispenser getting data : %s\n", rsp);
        usleep(30000);
    }
}

void *get2(void *args)
{
    int ret = -1;
    char rsp[128] = "";
    struct dispenser_config *dispenser = (struct dispenser_config *)args;

    while (1) {
        ret = dispenser_get_data(dispenser, rsp);  
        if (-1 == ret) {
            continue;
        }
        printf("[2]the dispenser getting data : %s\n", rsp);
        usleep(30000);
    }
}



int main()
{
    int ret= -1;
    struct dispenser_config dispenser = {0}; 
    pthread_t pset;
    pthread_t pget1;
    pthread_t pget2;

    ret = dispenser_shmt_init(&dispenser);
    if (-1 == ret) {
        perror("dispenser_shmt_init");
        return -1;
    }

    printf("init shmt\n");

    pthread_create(&pset, NULL, set, &dispenser);
    pthread_create(&pget1, NULL, get1, &dispenser);
    pthread_create(&pget2, NULL, get2, &dispenser);

    while (1) {
        sleep(2);
    }

    dispenser_shmt_del(&dispenser);
    return 0;
}
